//
//  GSDKMainViewController.m
//  GameSDK
//
//  Created by Johnny on 2017/8/7.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKMainViewController.h"
#import "GSDKCommonMethod.h"
#import "YYKit.h"
#import "GSDKWebViewController.h"
#import "UIColor+Extension.h"
#import "GSDKRequestModel.h"
@interface GSDKMainViewController ()
@property BOOL isReload;
@end

@implementation GSDKMainViewController
- (void)dealloc {
    DEBUGMSG(@"GSDKMainViewController dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (IS_VERSION_IOS7) {
        self.tabBar.tintColor = [UIColor colorWithHexString:@"#2eb3e8"];
    } else {
        self.tabBar.tintColor = [UIColor whiteColor];
        //去除tabbar的黑线和阴影
        [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
        [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
        [self.tabBar setSelectionIndicatorImage:[[UIImage alloc]init]];
    }
    
    _isReload = false;
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    for (int i = 0; i < [GSDKCOMMONMETHOD.menuModel.tool_bar count]; i++) {
        GSDKResToolbarModel * model = GSDKCOMMONMETHOD.menuModel.tool_bar[i];
        NSString * imageNamed = [NSString stringWithFormat:@"ZYSDK_Resourcres.bundle/pic/tabbar_%@", model.src];
        NSString * selectImageNamed = [NSString stringWithFormat:@"ZYSDK_Resourcres.bundle/pic/tabbar_%@_s.%@", [model.src componentsSeparatedByString:@"."].firstObject,[model.src componentsSeparatedByString:@"."].lastObject];
        
        GSDKWebViewController * vc = [[GSDKWebViewController alloc]init];
        if (IS_VERSION_IOS7) {
            self.tabBar.translucent = NO;
            vc.edgesForExtendedLayout = UIRectEdgeNone;
        }
        vc.tabBarItem.tag = i;
        [self addChildrenViewController:vc title:model.name image:imageNamed selectedImage:selectImageNamed];
        GSDKWebModel * wModel = [[GSDKWebModel alloc] initIAPCallBackWithSid:GSDKCOMMONMETHOD.GSDKSid == nil ? GSDKCOMMONMETHOD.GSDKSid : @"" andToken:GSDKCOMMONMETHOD.GSDKToken andLogin_token: GSDKCOMMONMETHOD.accountModel.token];
        NSString * urlString = model.url;
        if (![urlString hasSuffix:@"?"]) { urlString = [NSString stringWithFormat:@"%@?", urlString]; }
        [vc reloadAtUrl: [NSString stringWithFormat:@"%@%@", urlString, [wModel getUrlParameter]]];
    }
}

/**
 *  添加一个子控制器
 *
 *  @param child         子控制器
 *  @param title         标题
 *  @param image         图标
 *  @param selectedImage 选中时的图标
 */
- (void)addChildrenViewController :(UIViewController * )child title: (NSString *)title image :(NSString *)image selectedImage:(NSString *)selectedImage {
    child.tabBarItem.title = title;
    DEBUGMSG(@"selectedImage = %@",selectedImage);
    [child.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], UITextAttributeTextColor : [UIColor colorWithHexString:@"#2eb3e8"]} forState:UIControlStateSelected];
    [child.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13],UITextAttributeTextColor : [UIColor colorWithHexString:@"#959595"]} forState:UIControlStateNormal];
    UIImage * selected = [UIImage imageNamed:selectedImage];
    [child.view setBackgroundColor:[UIColor whiteColor]];
    if (IS_VERSION_IOS7) {
        child.tabBarItem.image = [UIImage imageNamed:image];
        child.tabBarItem.selectedImage = [selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        [child.tabBarItem setFinishedSelectedImage:selected withFinishedUnselectedImage:[UIImage imageNamed:image]];
    }
    
    child.title = title;
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:child];
    
    [self addChildViewController:nav];
}

@end
